package com.codingman.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//ResultSet
public class Demo3 {

	public static void main(String[] args) {
		// Open a connection
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");

			Connection con = DriverManager.getConnection("jdbc:mysql://45.77.187.28:3306/codingdb", "codingman",
					"Cm@123456");

			Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			ResultSet rs = stmt.executeQuery("select * from employee");

			// Move cursor to the last row.
			System.out.println("Moving cursor to the last...");
			rs.last();
			printResultSet(rs);

			// Move cursor to the first row.
			System.out.println("Moving cursor to the first row...");
			rs.first();
			printResultSet(rs);

			// Move cursor to the first row.
			System.out.println("Moving cursor to the next row...");
			rs.next();
			printResultSet(rs);

		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void printResultSet(ResultSet rs) throws SQLException {
		// Extract data from result set
		System.out.println("Displaying record...");

		int id = rs.getInt("id");
		String name = rs.getString("name");
		float salary = rs.getFloat("salary");
		
//		int id = rs.getInt(1);
//		String name = rs.getString(2);
//		float salary = rs.getFloat(3);

		// Display values
		System.out.print("ID: " + id);
		System.out.print(", Name: " + name);
		System.out.print(", Salary: " + salary);
	}
}
